package com.hckjsec.official.controller;

import java.util.List;

import com.hckjsec.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.official.domain.OfficialNews;
import com.hckjsec.official.service.IOfficialNewsService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 公司新闻Controller
 * 
 * @author cyt
 * @date 2021-12-09
 */
@RestController
@RequestMapping("/official/news")
public class OfficialNewsController extends BaseController
{
    @Autowired
    private IOfficialNewsService officialNewsService;

    /**
     * 查询公司新闻列表
     */
    @GetMapping("/list")
    public TableDataInfo list(OfficialNews officialNews)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            officialNews.setDeptId(SecurityUtils.getDeptId());
        }
        List<OfficialNews> list = officialNewsService.selectOfficialNewsList(officialNews);
        list.forEach(kv->{
            kv.setContent(null);
        });
        return getDataTable(list);
    }

    /**
     * 导出公司新闻列表
     */
    @Log(title = "公司新闻", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OfficialNews officialNews)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            officialNews.setDeptId(SecurityUtils.getDeptId());
        }
        List<OfficialNews> list = officialNewsService.selectOfficialNewsList(officialNews);
        ExcelUtil<OfficialNews> util = new ExcelUtil<OfficialNews>(OfficialNews.class);
        return util.exportExcel(list, "news");
    }

    /**
     * 获取公司新闻详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(officialNewsService.selectOfficialNewsById(id));
    }

    /**
     * 新增公司新闻
     */
    @Log(title = "公司新闻", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OfficialNews officialNews)
    {
        officialNews.setDeptId(SecurityUtils.getDeptId());
        officialNews.setCreateBy(SecurityUtils.getUsername());
        return toAjax(officialNewsService.insertOfficialNews(officialNews));
    }

    /**
     * 修改公司新闻
     */
    @Log(title = "公司新闻", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OfficialNews officialNews)
    {
        officialNews.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(officialNewsService.updateOfficialNews(officialNews));
    }

    /**
     * 删除公司新闻
     */
    @Log(title = "公司新闻", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(officialNewsService.deleteOfficialNewsByIds(ids));
    }
}
